//
//  ViewController.m
//  UI22_多线程
//
//  Created by Scott on 16/4/8.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"
#import "UIImageView+DownloadImage.h"

@interface ViewController ()

@property (nonatomic, retain) UIImage *imageOfHead;

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    [self loadImage];
    
   
    [self.imageView asynDownloadImageWithURL:@"http://img5.cache.netease.com/3g/2015/10/30/20151030124521c1f7e.jpg" completionHandle:^(NSData *data, UIImage *image) {
        
        NSLog(@"%f, %f", image.size.width, image.size.height);
    }];
    
    
//    [self chapterOfThread];
    
//    [self chapterOfNSOperationQueue];
    
//    [self chapterOfObject];
    
    [self chapterOfGCD];
    
    
    
}


#pragma mark - 代码块
- (void)snippet {
    
    NSLog(@"snippet's thread:%@", [NSThread currentThread]);
    
    
    NSInteger sum = 0;
    
    // 循环10亿次.
    for (NSInteger i = 0; i < 1000000000; i++) {
        
        sum += i;
    }
    
    NSLog(@"sum:%ld", sum);
}


#pragma mark - 知识点: GCD


#pragma mark ** GCD 创建
- (void)chapterOfGCD {
    
#if 0   // 创建串行队列.
    dispatch_queue_t queue = dispatch_queue_create("com.lanou3g", DISPATCH_QUEUE_SERIAL);
    
    
    dispatch_async(queue, ^{
        
        [self snippet];
    });
    
    
    dispatch_async(queue, ^{
        NSLog(@"ddd");
    });

#endif
    
    // 创建并发队列.
    dispatch_queue_t queue = dispatch_queue_create("dls160101.lanou3g", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        
        [self snippet];
    });
    
    
    dispatch_async(queue, ^{
        
        NSLog(@"ddd");
    });
    
}


#pragma mark ** GCD 应用场景 - 单例
+ (instancetype)sharedViewController {
    
    static ViewController *vc = nil;

    // 只运行一次.
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        vc = [[ViewController alloc] init];
    });
    
    return vc;
}

#pragma mark ** GCD 应用场景 - 图片异步下载
- (void)loadImage {
    
    // 在子线程运行下载图片代码.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img5.cache.netease.com/3g/2015/10/30/20151030124521c1f7e.jpg"]];
        
        
        // 返回主线程, UI赋值.
        dispatch_async(dispatch_get_main_queue(), ^{
            
            // Core API:
            self.imageOfHead = [UIImage imageWithData:data];
  
        });

    });

}

#pragma mark - 知识点: NSThread 
- (void)chapterOfThread {
    
    NSLog(@"chapterOfThread's thread: %@", [NSThread currentThread]);
    
    // 创建一个子线程
    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(snippet) object:nil];
    
    thread1.name = @"循环";
    
    // 手动开始线程.
    [thread1 start];
    
    
    // 子线程自动开启
    [NSThread detachNewThreadSelector:@selector(snippet) toTarget:self withObject:nil];

}

#pragma mark - 知识点: NSOperationQueue
- (void)chapterOfNSOperationQueue {
    
    // 操作队列 中是 操作对象
    
    NSInvocationOperation *ope1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(snippet) object:nil];
    
    NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(snippet) object:nil];
    
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        
        [self snippet];
        
    }];
    
    
    // Core API:
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    queue.maxConcurrentOperationCount = 2;
    
    [queue addOperation:ope1];
    
    [queue addOperation:op2];
    
    [queue addOperation:op3];
    
    
}

#pragma mark - 知识点: NSObject 
- (void)chapterOfObject {
    
    [self performSelectorInBackground:@selector(snippet) withObject:nil];
    
}























@end
